package org.infodavid.common.model.impl;

import org.infodavid.common.model.components.IGeoLocation;
import org.infodavid.common.model.impl.AbstractBasicDataObjectImpl;

/**
 * The Class GeoLocationImpl.
 */
public class GeoLocationImpl extends AbstractBasicDataObjectImpl implements IGeoLocation {

	/** serialVersionUID. */
	private static final long serialVersionUID = -7164736600775401440L;

	/** The longitude. */
	private float longitude;

	/** The latitude. */
	private float latitude;

	/**
	 * The Constructor.
	 * @throws Exception the exception
	 */
	protected GeoLocationImpl() throws Exception {
		super();
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.entities.IGeoLocation#setLatitude(float)
	 */
	public void setLatitude(final float value) {
		if (!equals(Float.valueOf(latitude), Float.valueOf(value))) {
			latitude = value;
			markModified(LATITUDE);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.entities.IGeoLocation#getLatitude()
	 */
	public float getLatitude() {
		return latitude;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.entities.IGeoLocation#setLongitude(float)
	 */
	public void setLongitude(final float value) {
		if (!equals(Float.valueOf(longitude), Float.valueOf(value))) {
			longitude = value;
			markModified(LONGITUDE);
		}
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.entities.IGeoLocation#getLongitude()
	 */
	public float getLongitude() {
		return longitude;
	}

	/*
	 * (non-Javadoc)
	 * @see org.infodavid.common.model.IBasicDataObject#init()
	 */
	@Override
	public void init() throws Exception {
		super.init();

		longitude = 0;
		latitude = 0;
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#hashCode()
	 */
	@Override
	public int hashCode() {
		final int prime = 31;
		int result = 1;

		result = prime * result + Float.floatToIntBits(getLatitude());

		return prime * result + Float.floatToIntBits(getLongitude());
	}

	/*
	 * (non-Javadoc)
	 * @see java.lang.Object#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(final Object obj) {
		if (this == obj) {
			return true;
		}

		if (obj == null) {
			return false;
		}

		if (!(obj instanceof IGeoLocation)) {
			return false;
		}

		final IGeoLocation other = (IGeoLocation)obj;

		if (Float.floatToIntBits(getLatitude()) != Float.floatToIntBits(other.getLatitude())) {
			return false;
		}

		if (Float.floatToIntBits(getLongitude()) != Float.floatToIntBits(other.getLongitude())) {
			return false;
		}

		return true;
	}

	/*
	 * See super class or interface. (non-Javadoc)
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		final StringBuilder builder = new StringBuilder();

		builder.append(super.toString());
		builder.append(" [latitude=");
		builder.append(latitude);
		builder.append(", longitude=");
		builder.append(longitude);
		builder.append("]");

		return builder.toString();
	}
}
